﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Hellquest
{
   /// <summary>
    /// Compnent that enables physically correct movement and interaction with force fields
    /// </summary>
    public class Rigidbody : Component
    {
        public float mass;

        public Vector2 velocity { get; private set; }
        private Vector2 acceleration;

        public Vector2 NewPosition { get { return newPosition; } }
        public Vector2 newPosition;
        public Vector2 newVelocity;
        private Vector2 newAcceleration;

        public Rigidbody()
        {
            mass = 1;
            IsActive = true;
            velocity = new Vector2();
            acceleration = new Vector2();

            ownPos = new Vector2();
            newPosition = new Vector2();
            newVelocity = new Vector2();
            newAcceleration = new Vector2();
        }

        public Rigidbody(float _mass)
        {
            mass = _mass;
            IsActive = true;

            ownPos = new Vector2();
            velocity = new Vector2();
            acceleration = new Vector2();

            newPosition = new Vector2();
            newVelocity = new Vector2();
            newAcceleration = new Vector2();
        }

        public override void OnAdd(GameObject _gameObject)
        {
            base.OnAdd(_gameObject);
            _gameObject.rigidbody = this;
            newPosition = gameObject.transform.WorldPosition;
            PhysicsEngine.Instance.AddRigidbody(this);
        }

        public override void OnRemove()
        {
            base.OnRemove();
            gameObject.rigidbody = null;
            PhysicsEngine.Instance.RemoveRigidbody(this);
        }

        /// <summary>
        /// Accelerates the rigidbody by the given force
        /// </summary>
        public void ApplyForce(Vector2 _force)
        {
            acceleration += _force / mass;
        }

        /// <summary>
        /// Influences velocity directly
        /// </summary>
        public void ApplyImpulse(Vector2 _velocity)
        {
            velocity = _velocity;
        }

        public void StartIntegration()
        {
            newPosition = Position;
            newVelocity = velocity;
            newAcceleration = acceleration;
        }

        public void IntegrateTimestep(float _timeDiff)
        {
            MathUtils.IntegrateVerlet(ref newAcceleration, ref newVelocity, ref newPosition, _timeDiff);
        }

        public void EndFrame()
        {
            Position = newPosition;
            velocity = newVelocity;
            acceleration = Vector2.Zero;
        }
    }
}
